﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADO.NET.Samples.Common;
using ADO.NET.Samples.DBML;

namespace ADO.NET.Samples.Forms.LINQ.LINQToSQL
{
    [FormDescription("LINQ to SQL Sample", "Demonstrate how to use LINQ to query data from SQL Server Database.")]
    public partial class LINQtoSQLSample : Form
    {
        public LINQtoSQLSample()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Data binding
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LINQtoSQLSample_Load(object sender, EventArgs e)
        {
            ArticleDataClassesDataContext dataContext = new ArticleDataClassesDataContext();
            var articles = from article in dataContext.Articles
                           where article.ArticleID < 5
                           select article;
            this.dataGridView1.DataSource = articles.AsEnumerable();

            var articles2 = from user in dataContext.Users
                            from article in user.Articles
                            let Author = user.UserName
                            orderby user.UserName descending
                            select new { Author, article.ArticleID, article.ArticleTitle, article.ArticleContent };
            this.dataGridView2.DataSource = articles2.AsEnumerable();
        }
    }
}
